package com.example.mvvmdemo.ui.activity;

import android.content.Intent;
import android.view.View;
import androidx.lifecycle.Observer;
import com.example.mvvmdemo.R;
import com.example.common.base.BaseActivity;
import com.example.mvvmdemo.databinding.ActivityProfileBinding;
import com.example.mvvmdemo.viewmodel.ProfileViewModel;
import com.example.common.utils.LocaleManager;
import com.example.common.utils.LogUtil;

public class ProfileActivity extends BaseActivity<ActivityProfileBinding, ProfileViewModel> {
    
    @Override
    protected int getLayoutId() {
        return R.layout.activity_profile;
    }
    
    @Override
    protected Class<ProfileViewModel> getViewModelClass() {
        return ProfileViewModel.class;
    }
    
    @Override
    protected void initView() {
        binding.setViewModel(viewModel);
        
        // 设置点击事件
        binding.layoutLanguage.setOnClickListener(v -> {
            LogUtil.d("ProfileActivity", "Language setting clicked");
            startActivity(new Intent(this, LanguageActivity.class));
        });
        
        binding.layoutSettings.setOnClickListener(v -> {
            LogUtil.d("ProfileActivity", "Settings clicked");
            // TODO: 跳转到设置页面
        });
        
        binding.layoutAbout.setOnClickListener(v -> {
            LogUtil.d("ProfileActivity", "About clicked");
            // TODO: 跳转到关于页面
        });
        
        binding.btnLogout.setOnClickListener(v -> {
            LogUtil.d("ProfileActivity", "Logout clicked");
            // TODO: 退出登录逻辑
        });
    }
    
    @Override
    protected void initData() {
        // 更新当前语言显示
        updateCurrentLanguageDisplay();
    }
    
    @Override
    protected void initObserver() {
        // 观察ViewModel数据变化
        viewModel.userName.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String userName) {
                LogUtil.d("ProfileActivity", "User name changed: " + userName);
            }
        });
        
        viewModel.userEmail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String userEmail) {
                LogUtil.d("ProfileActivity", "User email changed: " + userEmail);
            }
        });
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // 每次回到页面时更新语言显示
        updateCurrentLanguageDisplay();
    }
    
    private void updateCurrentLanguageDisplay() {
        String currentLanguage = LocaleManager.getCurrentLanguageDisplayName(this);
        viewModel.updateCurrentLanguage(currentLanguage);
        binding.tvCurrentLanguage.setText(currentLanguage);
        
        LogUtil.d("ProfileActivity", "Language display updated: " + currentLanguage);
    }
}